/*
 * 兴趣标签
 * @Author: along
 * @Date: 2019-12-23 09:34:00
 */
<template>
    <div class="container socialLabel">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-box">
                    <p class="header-box-title">标签分类:</p>
                    <el-input
                        v-model="header.label"
                        placeholder="请输入标签分类"
                        suffix-icon="el-icon-search"
                        size="small"
                        clearable
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
            </div>
            <div class="header-right">
                <div class="header-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="fnAddlabel()"
                    >新建分类</el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="socialLabel"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="group_name"
                label="标签分类"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="tag_count"
                label="子标签"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <span
                        @click="fnGoSocile(scope.row)"
                        class="cursor-font"
                    >{{ scope.row.tag_count }}</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="group_sort"
                label="排序"
            >
                <template slot-scope="scope">
                    <div class="sort-wrap">
                        {{ scope.row.group_sort }}
                        <span class="sort-spliter" />
                        <el-button
                            type="text"
                            class="btn-sort"
                            @click="fnEditSort(scope.row)"
                        >
                            <i class="el-icon-edit" />
                        </el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="create_time"
                min-width="110"
                label="属性"
                align="center"
            >
                <template slot-scope="scope">
                    <div v-if="scope.row.group_scope === 'visit_city'">去过的城市</div>
                    <div v-if="scope.row.group_scope === 'interest'">兴趣爱好</div>
                    <div v-if="scope.row.group_scope === 'other'">其它</div>
                    <div v-if="scope.row.group_scope === ''">--</div>
                </template>
            </el-table-column>
            <el-table-column
                prop="create_time"
                min-width="110"
                label="创建时间"
                align="center"
            >
                <template slot-scope="scope">
                    {{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                prop="group_show"
                label="状态"
            >
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.group_show"
                        :active-value="1"
                        :inactive-value="0"
                        @change="fnShowStatus(scope.row)"
                    />
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnEditLabel(scope.row)"
                    >
                        编辑
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnRemoveLabel(scope.row)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- 添加/编辑分类 -->
        <el-dialog
            :title="typeModel === 'add' ? '新建分类' : '分类编辑'"
            :visible.sync="dialogVisible"
            width="520px"
            :close-on-click-modal="false"
        >
            <el-form
                :model="ruleForm"
                :rules="rules"
                ref="ruleForm"
                label-width="100px"
                class="demo-ruleForm"
            >
                <el-form-item
                    label="分类名称"
                    prop="name"
                >
                    <el-input
                        size="small"
                        v-model="ruleForm.name"
                        placeholder="请输入分类名称,不超过6个字"
                        maxlength="6"
                    />
                </el-form-item>
                <el-form-item
                    label="分类属性"
                    prop="name"
                >
                    <el-select
                        v-model="ruleForm.group_scope"
                        placeholder="请选择分类属性"
                        size="small"
                        clearable
                        @change="$forceUpdate()"
                    >
                        <el-option
                            v-for="item in scopeList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
            </el-form>
            <span
                slot="footer"
                class="dialog-footer">
                <el-button
                    @click="dialogVisible = false"
                    size="small"
                >取 消</el-button>
                <el-button
                    type="primary"
                    size="small"
                    :disabled="isValue"
                    @click="fnSubmitForm('ruleForm')">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 添加/编辑标签 -->

        <!-- /分页 -->
        <!-- <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div> -->
    </div>
</template>

<script>
import { back } from '@/mixin/back';
export default {
    /**
     * @param  {Object}    authResult           //表单权限
     * @param  {Array}     tableData            //表单数据
     * @param  {Object}    page                 //表单分页
     * @param  {Object}    header               //表单检索字段
     * @param  {Array}     labelList            //分类二级联动数据
     * @param  {Boolean}   dialogVisible        //弹窗开关
     * @param  {Object}    ruleForm             //弹窗字段
     * @param  {Object}    rules                //弹窗字段验证规则
     * @param  {Array}     mainLabe             //主标签数据
     * @param  {String}    typeModel            //弹窗类型_添加/编辑 add:添加 edit:编辑
     * @param  {Boolean}   isValue              //弹窗确定按钮是否可点击
     */
    mixins: [back],
    data () {
        return {
            group_no: '',
            isValue: true,
            typeModel: '',
            mainLabe: [{
                value: '1',
                label: '生活'
            }],
            rules: {
                name: [
                    { required: true, message: '请输入标签名称,不可以为空', trigger: 'blur' }
                ]
            },
            ruleForm: {
                name: '',
                group_scope: ''
            },
            dialogVisible: false,
            labelList: [],
            header: {
                label: ''
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {},
            // 分类属性列表
            scopeList: [
                { label: '去过的城市', value: 'visit_city' },
                { label: '兴趣爱好', value: 'interest' },
                { label: '其它', value: 'other' }
            ]
        };
    },
    filters: {},
    computed: {},
    watch: {
        ruleForm: {
            deep: true,
            handler (val) {
                if (val.name !== '') {
                    this.isValue = false;
                } else {
                    this.isValue = true;
                }
            }
        },
        dialogVisible (val) {
            if (!val) {
                this.ruleForm = {
                    name: ''
                };
                this.$refs['ruleForm'].resetFields();
            }
        }
    },
    mounted () {
        this.authResult = this.$route.meta.authResult;
        this.fnBreadcrumb();
        this.getTableData();
    },
    methods: {
        /**
         * 设置表单header
         */
        fnBreadcrumb () {
            this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '社交'},{title: '兴趣标签'}]);
        },

        /**
         * 获取表格数据
         */
        getTableData () {
            this.$post('/student/student_tag_group%5Cget_list', {
                keyword: this.header.label
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    // this.page.now_page = resp.data.page.now_page;
                    // this.page.total_count = resp.data.page.total_count;
                    // this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 获取路由参数
         */
        fnParamUrl () {
            let query = this.$route.query;
        },

        /**
         * 跳转标签名称
         */
        fnGoSocile (row) {
            this.$router.replace({
                name: 'socialLabelName',
                query: {
                    group_no: row.group_no
                }
            });
        },

        /**
         *  排序
         */
        fnEditSort(row) {
            this.$prompt('修改排序', '', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^(0|\+?[1-9][0-9]*)$/,
                inputPlaceholder: '请输入序号',
                inputErrorMessage: '请输入正整数',
                closeOnClickModal: false,
            }).then(({ value }) => {
                this.$post('/student/student_tag_group%5Cmodify', {
                    group_no: row.group_no,
                    group_sort: value
                }, resp => {
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '修改成功',
                            center: true
                        });
                    } else {
                        this.$message({
                            type: 'waring',
                            message: resp.msg,
                            center: true
                        });
                    }
                });
            });
        },

        /**
         * 上架/下架
         */
        fnShowStatus (row) {
            this.$post('/student/student_tag_group%5Cmodify',{
                group_no: row.group_no,
                group_show: row.group_show
            },resp => {
                if (resp.code == 1) {
                    this.$message({
                        type: 'success',
                        message: '设置成功',
                        center: true
                    });
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'warning',
                        message: resp.msg,
                        center: true
                    });
                }
            });
        },

        /**
         * 添加标签
         */
        fnAddlabel () {
            this.typeModel = 'add';
            this.ruleForm.name = '';
            this.ruleForm.group_scope = '';
            this.dialogVisible = !this.dialogVisible;
        },

        /**
         * 编辑标签
         */
        fnEditLabel (row) {
            this.typeModel = 'edit';
            this.group_no = row.group_no;
            this.ruleForm.name = row.group_name;
            this.ruleForm.group_scope = row.group_scope;
            this.dialogVisible = !this.dialogVisible;
        },

        /**
         * 确认添加/添加_标签
         */
        fnSubmitForm (formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (this.typeModel == 'add') {
                        this.$post('/student/student_tag_group%5Cadd',{
                            group_name: this.ruleForm.name,
                            group_scope: this.ruleForm.group_scope
                        },resp => {
                            if (resp.code == 1) {
                                this.getTableData();
                                this.dialogVisible = !this.dialogVisible;
                                this.$message({
                                    type: 'success',
                                    message: '添加成功',
                                    center: true
                                });
                            } else if (resp.code == 0) {
                                this.$message({
                                    type: 'warning',
                                    message: resp.msg,
                                    center: true
                                });
                            }
                        });
                    } else if (this.typeModel == 'edit') {
                        this.$post('/student/student_tag_group%5Cmodify',{
                            group_no: this.group_no,
                            group_name: this.ruleForm.name,
                            group_scope: this.ruleForm.group_scope
                        },resp => {
                            if (resp.code == 1) {
                                this.getTableData();
                                this.dialogVisible = !this.dialogVisible;
                                this.$message({
                                    type: 'success',
                                    message: '编辑成功',
                                    center: true
                                });
                            } else if (resp.code == 0) {
                                this.$message({
                                    type: 'warning',
                                    message: resp.msg,
                                    center: true
                                });
                            }
                        });
                    } else {
                        return;
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

        /**
         * 删除标签
         */
        fnRemoveLabel (row) {
            if (Number(row.tag_count) > 0) {
                this.$message({
                    type: 'waring',
                    message: '分类下有标签，不可删除',
                    center: true
                });
                return;
            }

            this.$confirm('删除当前标签，是否继续？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/student_tag_group%5Cdelete',{
                    group_no: row.group_no
                },resp => {
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '删除成功',
                            center: true
                        });
                    } else if (resp.code == 0) {
                        this.$message({
                            type: 'warning',
                            message: resp.msg,
                            center: true
                        });
                    }
                });
            });
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
            }
            .header-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
</style>
<style lang="less">
    .socialLabel .el-table__body-wrapper .el-table__body .el-table__row td {
        border-right: none;
    }
    .socialLabel .el-table__header-wrapper .el-table__header .has-gutter tr th {
        border-right: none;
    }
    .socialLabel .el-table .el-table_1_column_6  .cell {
        position: relative;
    }
    .socialLabel .el-range-editor--small .el-range-separator{
        padding: 0 1px !important;
    }
</style>
